/*
 * Copyright 2011 John Alström
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package se.elva.slticket.dialog;

import se.elva.slticket.R;
import se.elva.slticket.repository.SavedStates;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SettingsDialog extends Dialog {

	private Button okButton;
	private Button prefixButton;
	private Context context;
	public final static String CHECKBOX_CONFIRM = "confirmCheckBox";
	public final static String CHECKBOX_PASSWORD = "passwordCheckBox";
	
	
	public SettingsDialog(Context context) {
		super(context);
		this.context = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		LayoutInflater factory = LayoutInflater.from(context);
		final View textEntryView = factory.inflate(R.layout.settingsdialog,null);
		this.setContentView(textEntryView);
		addChangePasswordButton();
		addOkButton();
		addChangePrefixButton();
		setConfirmCheckBox();
		setPasswordCheckBox();
	}
	
	android.view.View.OnClickListener mOkListener = new android.view.View.OnClickListener() {
		public void onClick(View v) {
			cancel();
		}
	};
	
	android.view.View.OnClickListener mChangePassowrdListener = new android.view.View.OnClickListener() {
		public void onClick(View v) {
			ChangePasswordDialog dialog = new ChangePasswordDialog(context);
			dialog.show();
			
			//cancel();
		}
	};
	
	android.view.View.OnClickListener mChangePrefixListener = new android.view.View.OnClickListener() {
		public void onClick(View v) {
			PrefixDialog dialog = new PrefixDialog(context);
			dialog.show();
			
			//cancel();
		}
	};
	
	private void setConfirmCheckBox(){
		CheckBox checkBox = (CheckBox)findViewById(R.id.checkBoxConfirmOnOff);
		checkBox.setOnCheckedChangeListener(onConfirmCheckbox);
		checkBox.setChecked(loadCheckBox(CHECKBOX_CONFIRM, true));
	}
	
	private void setPasswordCheckBox(){
		CheckBox checkBox = (CheckBox)findViewById(R.id.checkBoxPasswordOnOff);
		checkBox.setOnCheckedChangeListener(onPasswordCheckbox);
		checkBox.setChecked(loadCheckBox(CHECKBOX_PASSWORD, false));
	}
	
	private boolean loadCheckBox(String key,boolean def){
		SharedPreferences pref = context.getSharedPreferences("SLTicket", 0);
        return pref.getBoolean(key, def);
	}
	
	private void saveState(String key,boolean state){
	       
        SharedPreferences.Editor editor = context.getSharedPreferences("SLTicket", 0).edit();
        editor.putBoolean(key, state);
        editor.commit();
	}
	
	private OnCheckedChangeListener onConfirmCheckbox = new OnCheckedChangeListener() {
		
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(!isChecked){
				saveState(CHECKBOX_PASSWORD, false);
				setPasswordCheckBox();
			}
			saveState(CHECKBOX_CONFIRM,isChecked);
		}
	};
	private OnCheckedChangeListener onPasswordCheckbox = new OnCheckedChangeListener() {
		
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(isChecked){
				boolean before = loadCheckBox(CHECKBOX_PASSWORD,false);
				saveState(CHECKBOX_CONFIRM, true);
				setConfirmCheckBox();
				if(!before && new SavedStates(context).getSavedPassword().equals("")){
					//SHOW set password
					SetNewPasswordDialog p = new SetNewPasswordDialog(context);
					p.show();
				}
			}
			saveState(CHECKBOX_PASSWORD,isChecked);
		}
	};
	
	private void addChangePasswordButton(){
		this.okButton = (Button)findViewById(R.id.changePassword);
		this.okButton.setOnClickListener(mChangePassowrdListener);
	}
	
	private void addChangePrefixButton(){
		this.prefixButton = (Button)findViewById(R.id.changePrefix);
		this.prefixButton.setOnClickListener(mChangePrefixListener);
		
	}

	private void addOkButton(){
		this.okButton = (Button)findViewById(R.id.ok);
		this.okButton.setOnClickListener(mOkListener);
	}

}
